﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;

namespace NExt.Handler.Component.Default.JsSerialize
{
    public abstract class AbstractNExtScriptGenerator : INExtScriptGenerator
    {
        private readonly string scriptPostfix;

        public AbstractNExtScriptGenerator(string scriptPostfix)
        {
            this.scriptPostfix = scriptPostfix;
        }

        public string Build(Type type)
        {
            Assembly assembly = type.Assembly;
            string fullName = type.FullName;

            string scriptResourceName = string.Format("{0}.{1}", fullName, scriptPostfix);

            Stream stream = assembly.GetManifestResourceStream(scriptResourceName);
            if (stream == null)
                return BuildDefaultContent(type);

            StreamReader streamReader = new StreamReader(stream);
            string content = streamReader.ReadToEnd();
            return content;
        }

        protected abstract string BuildDefaultContent(Type type);
    }
}
